﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.Sudung
{
    public partial class popNhapso : Form
    {
        public popNhapso()
        {
            InitializeComponent();
        }
        private void popNhapso_Load(object sender, EventArgs e)
        {
            //Autocomplete
            clKhachhang kh = new clKhachhang();
            AutoCompleteStringCollection dskhachhang = new AutoCompleteStringCollection();
            DataTable dtds = kh.getautocomplete(0, 0);
            if (dtds != null && dtds.Rows.Count > 0)
            {
                foreach (DataRow row in dtds.Rows)
                {
                    dskhachhang.Add(row["maCu"].ToString());
                }
            }
            txtMaKh.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtMaKh.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtMaKh.AutoCompleteCustomSource = dskhachhang;
        }

        public delegate void Nhapso(int id);
        // khai báo 1 kiểu hàm delegate
        public Nhapso MyGetData;

        //private void btsave_Click(object sender, EventArgs e)
        //{
        //    if (MyGetData != null)
        //    {
        //        string sohd = txtsohd.Text.Trim();
        //        Int64 ngaydoc = Int64.Parse(dateday.Text.Trim().Split('/')[2].ToString() + dateday.Text.Trim().Split('/')[1].ToString() + dateday.Text.Trim().Split('/')[0].ToString());
        //        int socu = int.Parse((txtsocu.Text.Trim().Length > 0) ? txtsocu.Text.Trim() : "0");
        //        int somoi = int.Parse((txtsomoi.Text.Trim().Length > 0) ? txtsomoi.Text.Trim() : "0");
        //        string nvten = DataUser.UserName;
        //        clSudung clsd = new clSudung();
        //        int outid = clsd.addsudung(sohd, ngaydoc, socu, somoi, nvten);
        //        MyGetData(outid);
        //    }
        //    this.Close();
        //}

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMaKh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                txtsomoi.Focus();
            }
        }

        private void txtsomoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnsave.Select();
                SendKeys.Send("{enter}");
                //this.AcceptButton = btnsave;
            }
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (MyGetData != null)
            {
                string maCu = txtMaKh.Text.Trim();
                Int64 ngaydoc = Int64.Parse(dateday.Text.Trim().Split('/')[2].ToString() + dateday.Text.Trim().Split('/')[1].ToString() + dateday.Text.Trim().Split('/')[0].ToString());
                int socu = int.Parse((txtsocu.Text.Trim().Length > 0) ? txtsocu.Text.Trim() : "0");
                int somoi = int.Parse((txtsomoi.Text.Trim().Length > 0) ? txtsomoi.Text.Trim() : "0");
                string nvten = DataUser.UserName;
                clSudung clsd = new clSudung();
                int outid = clsd.addsudung(maCu, ngaydoc, socu, somoi, nvten);
                MyGetData(outid);
            }
            txtMaKh.Text = "";
            txtsocu.Text = "";
            txtsomoi.Text = "";
            lbtenKh.Text = "";
            lbmakH.Text = "";
            lbDiachi.Text = "";
            txtMaKh.Focus();
        }

        int BinSeach(int[] a, int gt, int dau, int cuoi)
        {
            int i, j;
            i = dau; j = cuoi;
            if (i > j)
            {
                return -1;
            }
            int giua = (i + j) / 2;
            if (gt == a[giua])
            {
             
                return giua;
            }
            else if (gt < a[giua])
            {
                j = giua - 1;
                return BinSeach(a, gt, dau, j);
            }
            else
            {
                i = giua + 1;
                return BinSeach(a, gt, i, cuoi);
            }
        }

        //public int find(DataTable a, int n, int x)
        //{
        //    int i = 0;
        //    while (i < n)
        //    {
        //        int temp = (n + i) / 2;
        //        if (a.Rows[i].ToString() < x)
        //            n = temp;
        //        else
        //            if (a[temp] > x)
        //                i = temp + 1;
        //            else
        //                return temp;
        //    }
        //    return 0;
        //}


        private void txtMaKh_TextChanged(object sender, EventArgs e)
        {
            int i;
            string makh = txtMaKh.Text;
            clSudung khsd = new clSudung();
            DataTable dtkhsd = khsd.sdSudung();
            for (i = 0; i < dtkhsd.Rows.Count; i++)
            {
                DataRow dr = dtkhsd.Rows[i];
                if (dr["maCu"].ToString() == makh)
                {
                    txtsomoi.Text = "";
                    txtsocu.Text = dr["cu2"].ToString();

                    lbmakH.Text = txtMaKh.Text;
                    lbtenKh.Text = dr["tenKH"].ToString();
                    lbDiachi.Text = dr["diachi"].ToString();
                }
            }
            //clSudung khsd = new clSudung();
            //DataTable dtkhsd = khsd.sdSudung();
            
            //int i = 0;
            //int j = dtkhsd.Rows.Count;
            //string makh = txtMaKh.Text;
        }
    }
}
